package org.freez4.spring.bean.definition;

import org.freez4.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.SimpleAliasRegistry;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Bean 别名demo。
 * <p>
 * 其底层实现是通过{@link ConfigurableListableBeanFactory#registerAlias(String, String)} API接口实现，
 * 在内部中使用{@link SimpleAliasRegistry#aliasMap}存储这些数据映射关系。
 *
 * @author 阿坤同学
 * @date 2023/12/6 3:36 PM
 */
public class BeanAliasDemo {

    private static final Logger logger = Logger.getLogger(BeanAliasDemo.class.getName());

    public static void main(String[] args) {
        ClassPathXmlApplicationContext beanFactory = new ClassPathXmlApplicationContext("classpath:/META-INF/bean-definitions-context.xml");
        User freeUser = beanFactory.getBean("free-user", User.class);
        logger.log(Level.INFO, () -> "XML 方式设置别名: " + freeUser);
    }

}
